package src;

public class MixColumns {
    public int judge_primacy(byte a){
        byte a02, trans_a;
        a02 = (byte)(a * 0x02);
        if(Byte.toUnsignedInt(a) >= 0x80) {
            trans_a = (byte)(a02 ^ 0x1b);
        }
        else {
            trans_a = a02;
        }
        return trans_a;
    }

    private final byte[] transformed_matrix = new byte[16];
    public void mix_columns(byte[] matrix){
        transformed_matrix[0] = (byte) ((judge_primacy(matrix[0])) ^ (judge_primacy(matrix[1]) ^ matrix[1]) ^ (matrix[2]) ^ (matrix[3]));
        transformed_matrix[1] = (byte) ((matrix[0]) ^ (judge_primacy(matrix[1])) ^ (judge_primacy(matrix[2]) ^ matrix[2]) ^ (matrix[3]));
        transformed_matrix[2] = (byte) ((matrix[0]) ^ (matrix[1]) ^ (judge_primacy(matrix[2])) ^ (judge_primacy(matrix[3]) ^ matrix[3]));
        transformed_matrix[3] = (byte) ((judge_primacy(matrix[0]) ^ matrix[0]) ^ (matrix[1]) ^ (matrix[2]) ^ (judge_primacy(matrix[3])));

        transformed_matrix[4] = (byte) ((judge_primacy(matrix[4])) ^ (judge_primacy(matrix[5]) ^ matrix[5]) ^ (matrix[6]) ^ (matrix[7]));
        transformed_matrix[5] = (byte) ((matrix[4]) ^ (judge_primacy(matrix[5])) ^ (judge_primacy(matrix[6]) ^ matrix[6]) ^ (matrix[7]));
        transformed_matrix[6] = (byte) ((matrix[4]) ^ (matrix[5]) ^ (judge_primacy(matrix[6])) ^ (judge_primacy(matrix[7]) ^ matrix[7]));
        transformed_matrix[7] = (byte) ((judge_primacy(matrix[4]) ^ matrix[4]) ^ (matrix[5]) ^ (matrix[6]) ^ (judge_primacy(matrix[7])));

        transformed_matrix[8] = (byte) ((judge_primacy(matrix[8])) ^ (judge_primacy(matrix[9]) ^ matrix[9]) ^ (matrix[10]) ^ (matrix[11]));
        transformed_matrix[9] = (byte) ((matrix[8]) ^ (judge_primacy(matrix[9])) ^ (judge_primacy(matrix[10]) ^ matrix[10]) ^ (matrix[11]));
        transformed_matrix[10] = (byte) ((matrix[8]) ^ (matrix[9]) ^ (judge_primacy(matrix[10])) ^ (judge_primacy(matrix[11]) ^ matrix[11]));
        transformed_matrix[11] = (byte) ((judge_primacy(matrix[8]) ^ matrix[8]) ^ (matrix[9]) ^ (matrix[10]) ^ (judge_primacy(matrix[11])));

        transformed_matrix[12] = (byte) ((judge_primacy(matrix[12])) ^ (judge_primacy(matrix[13]) ^ matrix[13]) ^ (matrix[14]) ^ (matrix[15]));
        transformed_matrix[13] = (byte) ((matrix[12]) ^ (judge_primacy(matrix[13])) ^ (judge_primacy(matrix[14]) ^ matrix[14]) ^ (matrix[15]));
        transformed_matrix[14] = (byte) ((matrix[12]) ^ (matrix[13]) ^ (judge_primacy(matrix[14])) ^ (judge_primacy(matrix[15]) ^ matrix[15]));
        transformed_matrix[15] = (byte) ((judge_primacy(matrix[12]) ^ matrix[12]) ^ (matrix[13]) ^ (matrix[14]) ^ (judge_primacy(matrix[15])));

        System.arraycopy(transformed_matrix, 0, matrix, 0 , 16);
    }
}
